//
//  IHandler.swift
//  DutyChainPattern
//
//  Created by mac on 2017/11/7.
//  Copyright © 2017年 RRC. All rights reserved.
//

import Foundation

//处理请求接口
protocol IHandler {
    //处理请求
    func HandleMessage(women:IWomen)
}


public class Handler{
    
    public static let FATHER_LEVEL_REQUEST = 1
    public static let HUSBAND_LEVEL_REQUEST = 2
    public static let SON_LEVEL_REQUEST = 3
    
    //能处理的级别
    private var level = 0
    //责任传递，下一个责任人是谁
    private var nextHandler:Handler?
    //每个类都要说明一下自己能处理哪些请求
    init(level:Int) {
        self.level = level
    }
    
    final func HandleMessage(women:IWomen){
        if women.getType() == self.level {
            self.response(women: women)
        }else{
            if (self.nextHandler != nil) {
                self.nextHandler?.HandleMessage(women: women)
            }else{
                print("-------没地方请示了，按不同意处理---------")
            }
        }
    }
    
    /**
     如果处理不了当前请求，就执行下一步去处理请求
     */
    func setNext(handle:Handler){
        self.nextHandler = handle
    }
    
    //有请求就要有回应
    func response(women:IWomen){}
}




//父亲类
public class Father:Handler,IHandler{
    //未出嫁的女儿来请示父亲
//    func HandleMessage(women: IWomen) {
//        print("女儿的请示是："+women.getRequest())
//        print("父亲的答复是：同意")
//    }
    
    ///修改后
    //父亲只处理女儿的请求
    init() {
        super.init(level: Handler.FATHER_LEVEL_REQUEST)
    }
    
    override func response(women: IWomen) {
        print("--------女儿向父亲请求---------")
        print(women.getRequest())
        print("父亲的答复是：同意")
    }
    
}

//丈夫类
public class Husband:Handler,IHandler {
    //妻子向丈夫请示
//    func HandleMessage(women: IWomen) {
//        print("妻子的请示是："+women.getRequest())
//        print("丈夫的答复是：同意")
//    }
    
    //////修改后
    //丈夫处理妻子的请求
    init() {
        super.init(level: Handler.HUSBAND_LEVEL_REQUEST)
    }
    
    //丈夫请示的答复
    override func response(women: IWomen) {
        print("----------妻子向丈夫请求--------")
        print(women.getRequest())
        print("丈夫的答复是：同意")
    }
    
}

//儿子类
public class Son:Handler,IHandler{
    //母亲向儿子请示
//    func HandleMessage(women: IWomen) {
//        print("母亲的请示是："+women.getRequest())
//        print("儿子的答复是：同意")
//    }
    
    //儿子处理母亲的请求
    init() {
        super.init(level: Handler.SON_LEVEL_REQUEST)
    }
    //儿子的答复
    override func response(women: IWomen) {
        print("------------母亲向儿子请示-----------")
        print(women.getRequest())
        print("儿子的答复是：同意")
    }
}
